package com.wjh.controler;

import com.wjh.bean.Student2;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/")
public class Student2Controler {
    /*
         * This method will serve as default GET handler.
         *
         */
    @ModelAttribute("student2")
    public Student2 getStudent2(){
        return new Student2();
    }
    @RequestMapping("student2")
    public String newRegistration(ModelMap model) {
//        Student2 student2 = new Student2();
//        model.addAttribute("student2", student2);
        return "enroll";
    }

    /*
     * This method will be called on form submission, handling POST request
     * It also validates the user input
     */
    @RequestMapping(value = "student2",method = RequestMethod.POST)
    public String saveRegistration(@Valid Student2 student2, BindingResult result, ModelMap model){

        if(result.hasErrors()) {
            return "enroll";
        }

        model.addAttribute("success", "Dear "+ student2.getFirstName()+" , your Registration completed successfully");
        return "success";
    }

    /*
     * Method used to populate the Section list in view.
     * Note that here you can call external systems to provide real data.
     */
    @ModelAttribute("sections")
    public List<String> initializeSections() {

        List<String> sections = new ArrayList<String>();
        sections.add("Graduate");
        sections.add("Post Graduate");
        sections.add("Research");
        return sections;
    }

    /*
     * Method used to populate the country list in view.
     * Note that here you can call external systems to provide real data.
     */
    @ModelAttribute("countries")
    public List<String> initializeCountries() {

        List<String> countries = new ArrayList<String>();
        countries.add("USA");
        countries.add("CHINA");
        countries.add("FRANCE");
        countries.add("GERMANY");
        countries.add("ITALY");
        countries.add("OTHER");
        return countries;
    }

    /*
     * Method used to populate the subjects list in view.
     * Note that here you can call external systems to provide real data.
     */
    @ModelAttribute("subjects")
    public List<String> initializeSubjects() {

        List<String> subjects = new ArrayList<String>();
        subjects.add("Physics");
        subjects.add("Chemistry");
        subjects.add("Life Science");
        subjects.add("Political Science");
        subjects.add("Computer Science");
        subjects.add("Mathmatics");
        return subjects;
    }

} 